<!DOCTYPE html>
<html lang="zh-CN" class="loading">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>使用阿里云函数计算免费创建自己的音乐播放器API - Skyone-Blog</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Node.js,函数计算,Server Less,"> 
    <meta name="description" content="Skyone技术博客,通过不算很难的三步，用阿里云函数计算FC免费创建自己的音乐播放器API，当然，你也可以用这种方法创建许多有趣的API甚至是网站。首先，你需要一个已经备案的域名，一个阿里云账号，自己要会JavaScript、了解ejs模板。"> 
    <meta name="author" content="王志伟"> 
    <link rel="alternative" href="atom.xml" title="Skyone-Blog" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/css/diaspora.css">

    <!-- 看板娘
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">
    <script src="/live2d-widget/autoload.js"></script>
    -->
    <!-- MD5 -->
    <script src="/js/md5.min.js"></script>
    <!-- MathJax -->
    
        <script type="text/x-mathjax-config">
        (function () {
            let target = document.getElementsByClassName("content")[0];
            MathJax.Hub.Config({
                tex2jax: {
                    inlineMath:  [ ["$", "$"] ],
                    displayMath: [ ["$$","$$"] ],
                    skipTags: ['script', 'noscript', 'style', 'textarea', 'pre','code','a'],
                    ignoreClass:"class1"
                }
            });
            MathJax.Hub.Queue(["Typeset",MathJax.Hub,target]);
        })();
    </script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

    
<meta name="generator" content="Hexo 5.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Skyone-Blog</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://www.skyone.host"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">使用阿里云函数计算免费创建自己的音乐播放器API</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">使用阿里云函数计算免费创建自己的音乐播放器API</h1>
        <div class="stuff">
            <span>三月 06, 2021</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Node-js/" rel="tag">Node.js</a></li></ul>


        </div>
        <div class="content markdown">
            <iframe style="border: none; width: 100%; height: 110px" src="https://music.luotianyi.space:19375/?_=|https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/music/夜航星%20-%20洛天依.mp3|https://ooo.0x0.ooo/2021/02/14/o08AS.png||https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/lrc/夜航星%20-%20洛天依.lrc"></iframe>

<p>这篇文章将一步一步的带你搭建一个播放器，也就是提供GET请求，返回一个带有播放器的网页，</p>
<p>你可以用 <code>iframe</code> 元素将这个播放器嵌入任何支持HTML语法的地方（MarkDown支持全部HTML语法）。</p>
<p>这个过程几乎是免费的，除非你的需求很大。当然，只是嵌入自己的文章的话，阿里云提供的免费额度是肯定够用的。</p>
<p><strong>感谢APlayer的作者！</strong></p>
<p>当然，利用这种方法，你可以做出许多有趣的API，快试试吧~</p>
<h2 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h2><h3 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h3><p>写一个GET请求API，参数自己设计，根据参数自动生成对应的歌曲的音乐播放器页面。</p>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>首先，你需要</p>
<ul>
<li><strong>一个已经备案的域名</strong></li>
<li>一个阿里云账号</li>
</ul>
<p>此外，技术栈：</p>
<ul>
<li>有 JavaScript 基础</li>
<li>了解 ejs 模板引擎，非必要，只是用来生成HTML代码</li>
</ul>
<h3 id="完成示例"><a href="#完成示例" class="headerlink" title="完成示例"></a>完成示例</h3><p>例如<a target="_blank" rel="noopener" href="https://music.luotianyi.space:19375/?_=%7Chttps://cdn.jsdelivr.net/gh/skyone-res/static@master/res/music/%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.mp3%7Chttps://ooo.0x0.ooo/2021/02/14/o08AS.png%7C%7Chttps://cdn.jsdelivr.net/gh/skyone-res/static@master/res/lrc/%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.lrc">这个网页</a><del>GitHub+JsDeliver大法</del></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:&#x2F;&#x2F;music.luotianyi.space:19375&#x2F;?_&#x3D;|https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;gh&#x2F;skyone-res&#x2F;static@master&#x2F;res&#x2F;music&#x2F;夜航星%20-%20洛天依.mp3|https:&#x2F;&#x2F;ooo.0x0.ooo&#x2F;2021&#x2F;02&#x2F;14&#x2F;o08AS.png||https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;gh&#x2F;skyone-res&#x2F;static@master&#x2F;res&#x2F;lrc&#x2F;夜航星%20-%20洛天依.lrc</span><br></pre></td></tr></table></figure>
<p>你可以将其嵌入你的文章，例如本篇文章开头的音乐播放器就是这么实现的。</p>
<h2 id="写代码"><a href="#写代码" class="headerlink" title="写代码"></a>写代码</h2><h3 id="看文档"><a href="#看文档" class="headerlink" title="看文档"></a>看文档</h3><p>我们使用Node.js开发HTTP函数，首先看看文档吧：<a target="_blank" rel="noopener" href="https://help.aliyun.com/document_detail/74757.html">https://help.aliyun.com/document_detail/74757.html</a></p>
<p>文档总结：</p>
<p>入口函数：<code>function(request, response, context)</code></p>
<p>其中：</p>
<ol>
<li><p><code>request</code></p>
<table>
<thead>
<tr>
<th align="left">字段</th>
<th align="left">类型</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>headers</code></td>
<td align="left">Object类型</td>
<td align="left">存放来自HTTP客户端的键值对。</td>
</tr>
<tr>
<td align="left"><code>path</code></td>
<td align="left">String类型</td>
<td align="left">表示HTTP路径。</td>
</tr>
<tr>
<td align="left"><code>queries</code></td>
<td align="left">Object类型</td>
<td align="left">存放来自HTTP路径中的查询部分的键值对，值的类型可以为字符串或数组。</td>
</tr>
<tr>
<td align="left"><code>method</code></td>
<td align="left">String类型</td>
<td align="left">表示HTTP方法。</td>
</tr>
<tr>
<td align="left"><code>clientIP</code></td>
<td align="left">String类型</td>
<td align="left">客户端的IP地址。</td>
</tr>
<tr>
<td align="left"><code>url</code></td>
<td align="left">String类型</td>
<td align="left">请求的地址。</td>
</tr>
</tbody></table>
<p>简单易懂，我就不多说了。</p>
</li>
<li><p><code>response</code></p>
<p>我们的目标很简单：组装一个网页并返回，所以我们只需要知道，返回网页有以下步骤</p>
<ul>
<li>使用 <code>response.setStatusCode(&quot;200&quot;)</code> 设置HTTP状态码为200，<strong>参数是字符串</strong></li>
<li>使用 <code>response.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;)</code> 提醒浏览器我们返回的是HTML代码</li>
<li>使用 <code>response.send(&quot;返回的网页代码&quot;)</code> 设置返回的HTML代码</li>
</ul>
</li>
<li><p><code>context</code></p>
<p>上下文，别问，我也不懂</p>
</li>
</ol>
<p>了解这么多就够了，例子太复杂，就不看了</p>
<h3 id="写模板"><a href="#写模板" class="headerlink" title="写模板"></a>写模板</h3><p>打开你的IDE，比如我用的WebStorm<del>学生党的除了没钱福利还是很多滴，感谢JetBrains！</del></p>
<p>新建ejs模板文件 <code>music.ejs</code> ，写个简单的模板，比如用APlayer这个轮子，如果不会<del>或者懒</del>就直接复制我的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;zh-CN&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;title&gt;Skyone - MusicPlayer&lt;&#x2F;title&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;npm&#x2F;aplayer@1.10.1&#x2F;dist&#x2F;APlayer.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;link rel&#x3D;&quot;stylesheet&quot; href&#x3D;&quot;https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;npm&#x2F;aplayer@1.10.1&#x2F;dist&#x2F;APlayer.min.css&quot;&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        html::-webkit-scrollbar &#123; display: none &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;body style&#x3D;&quot;margin: 0&quot;&gt;</span><br><span class="line">&lt;div id&#x3D;&quot;player&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    const ap &#x3D; new APlayer(&#123;</span><br><span class="line">        container: document.getElementById(&#39;player&#39;),</span><br><span class="line">        listFolded: true,</span><br><span class="line">        listMaxHeight: 70,</span><br><span class="line">        lrcType: 3,</span><br><span class="line">        audio: [</span><br><span class="line">            &lt;% for (let music of list) &#123; %&gt;</span><br><span class="line">            &#123;</span><br><span class="line">                name: &quot;&lt;%&#x3D; music.name %&gt;&quot;,</span><br><span class="line">                artist: &quot;&lt;%&#x3D; music.artist %&gt;&quot;,</span><br><span class="line">                url: &quot;&lt;%&#x3D; music.url %&gt;&quot;,</span><br><span class="line">                &lt;% if (music.cover)  &#123; %&gt;</span><br><span class="line">                cover: &quot;&lt;%&#x3D; music.cover %&gt;&quot;,</span><br><span class="line">                &lt;% &#125; %&gt;</span><br><span class="line">                &lt;% if (music.lrc)  &#123; %&gt;</span><br><span class="line">                lrc: &quot;&lt;%&#x3D; music.lrc %&gt;&quot;,</span><br><span class="line">                lrcType: 3,</span><br><span class="line">                &lt;% &#125; %&gt;</span><br><span class="line">            &#125;,</span><br><span class="line">            &lt;% &#125; %&gt;</span><br><span class="line">        ]</span><br><span class="line">    &#125;);</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>
<h3 id="安装Node-js的包"><a href="#安装Node-js的包" class="headerlink" title="安装Node.js的包"></a>安装Node.js的包</h3><p>CMD打开文件夹，输入以下命令来安装ejs，有cnpm的改用cnpm，不懂就复制粘贴</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install ejs</span><br></pre></td></tr></table></figure>
<h3 id="主函数"><a href="#主函数" class="headerlink" title="主函数"></a>主函数</h3><p>咳，主函数<del>就一个函数</del>，创建文件<code>index.js</code> ，导入ejs</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ejs = <span class="built_in">require</span>(<span class="string">&quot;ejs&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&quot;fs&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&quot;path&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>先写入口函数，根据你设计的GET请求API分析GET参数，把参数处理成对应的ejs数据，例如我给的ejs模板需要：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> list = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&quot;歌曲名称&quot;</span>,</span><br><span class="line">        url: <span class="string">&quot;歌曲链接&quot;</span>,</span><br><span class="line">        cover: <span class="string">&quot;专辑图片链接，可空&quot;</span> || <span class="literal">null</span>,</span><br><span class="line">        artist: <span class="string">&quot;歌手，可空&quot;</span> || <span class="string">&quot;未知&quot;</span>,</span><br><span class="line">        lrc: <span class="string">&quot;lrc歌词，可空&quot;</span> || <span class="literal">null</span></span><br><span class="line">    &#125;, &#123;</span><br><span class="line">        name: <span class="string">&quot;夜航星&quot;</span>,</span><br><span class="line">        url: <span class="string">&quot;https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/music/夜航星%20-%20洛天依.mp3&quot;</span>,</span><br><span class="line">        cover: <span class="string">&quot;https://ooo.0x0.ooo/2021/02/14/o08AS.png&quot;</span> || <span class="literal">null</span>,</span><br><span class="line">        artist: <span class="string">&quot;洛天依&quot;</span> || <span class="string">&quot;未知&quot;</span>,</span><br><span class="line">        lrc: <span class="string">&quot;https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/lrc/夜航星%20-%20洛天依.lrc&quot;</span> || <span class="literal">null</span></span><br><span class="line">    &#125;,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p>然后，设置header和statuscode，使用ejs模板生成网页并调用 <code>send()</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">response.setStatusCode(<span class="string">&quot;200&quot;</span>);</span><br><span class="line">response.setHeader(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;text/html&quot;</span>);</span><br><span class="line">response.send(ejs.render(fs.readFileSync(path.join(__dirname, <span class="string">&quot;music.ejs&quot;</span>)).toString(), &#123; list &#125;));</span><br></pre></td></tr></table></figure>
<p>同样，我给个完整的例子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&quot;use strict&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&quot;path&quot;</span>)</span><br><span class="line"><span class="keyword">const</span> ejs = <span class="built_in">require</span>(<span class="string">&quot;ejs&quot;</span>)</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&quot;fs&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.handler = <span class="function">(<span class="params">req, res, context</span>) =&gt;</span> &#123;</span><br><span class="line">    res.setHeader(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;text/html&quot;</span>)</span><br><span class="line">    <span class="keyword">const</span> params = &#123;</span><br><span class="line">        path: req.path,</span><br><span class="line">        queries: req.queries,</span><br><span class="line">        headers: req.headers,</span><br><span class="line">        method: req.method,</span><br><span class="line">        requestURI: req.url,</span><br><span class="line">        clientIP: req.clientIP,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">if</span> (params.queries[<span class="string">&#x27;_&#x27;</span>] &amp;&amp; params.queries[<span class="string">&quot;_&quot;</span>].indexOf(<span class="string">&quot;|&quot;</span>) === -<span class="number">1</span>) &#123;</span><br><span class="line">        res.setStatusCode(<span class="string">&#x27;400&#x27;</span>);</span><br><span class="line">        res.send(<span class="string">&quot;Params Error&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> data = params.queries[<span class="string">&quot;_&quot;</span>].split(<span class="string">&quot;^&quot;</span>)</span><br><span class="line">        <span class="keyword">let</span> list = [];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i <span class="keyword">of</span> data) &#123;</span><br><span class="line">            <span class="keyword">const</span> query = i.split(<span class="string">&quot;|&quot;</span>)</span><br><span class="line">            list[list.length] = &#123;</span><br><span class="line">                name: query[<span class="number">0</span>],</span><br><span class="line">                url: query[<span class="number">1</span>],</span><br><span class="line">                cover: query[<span class="number">2</span>] || <span class="literal">null</span>,</span><br><span class="line">                artist: query[<span class="number">3</span>] || <span class="string">&quot;未知&quot;</span>,</span><br><span class="line">                lrc: query[<span class="number">4</span>] || <span class="literal">null</span></span><br><span class="line">            &#125;;</span><br><span class="line">            <span class="keyword">let</span> filename = query[<span class="number">1</span>].split(<span class="string">&quot;/&quot;</span>).pop().split(<span class="string">&quot;.&quot;</span>)[<span class="number">0</span>].split(<span class="string">&quot; - &quot;</span>);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (filename.length === <span class="number">2</span>) &#123;</span><br><span class="line">                    list[list.length - <span class="number">1</span>].name = filename[<span class="number">0</span>];</span><br><span class="line">                    list[list.length - <span class="number">1</span>].artist = filename[<span class="number">1</span>];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">                res.setStatusCode(<span class="number">503</span>);</span><br><span class="line">                res.send(e);</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        res.setStatusCode(<span class="number">200</span>);</span><br><span class="line">        res.send(ejs.render(fs.readFileSync(path.join(__dirname, <span class="string">&quot;music.ejs&quot;</span>)).toString(), &#123; list &#125;));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">        res.setStatusCode(<span class="number">503</span>);</span><br><span class="line">        res.send(<span class="string">&quot;Server Error&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="我的API的参数"><a href="#我的API的参数" class="headerlink" title="我的API的参数"></a>我的API的参数</h3><p>就一个 <code>_</code> ，值为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">名称(支持根据url自动获取:歌曲-歌手.xxx)|歌曲url|专辑图片url|歌手(支持根据url自动获取)|歌词url</span><br></pre></td></tr></table></figure>
<p>多首歌通过 <code>^</code> 分隔，例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">?_&#x3D;|https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;gh&#x2F;skyone-res&#x2F;static@master&#x2F;res&#x2F;music&#x2F;%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.mp3|https:&#x2F;&#x2F;ooo.0x0.ooo&#x2F;2021&#x2F;02&#x2F;14&#x2F;o08AS.png||https:&#x2F;&#x2F;cdn.jsdelivr.net&#x2F;gh&#x2F;skyone-res&#x2F;static@master&#x2F;res&#x2F;lrc&#x2F;%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.lrc</span><br></pre></td></tr></table></figure>
<h3 id="保存、打包"><a href="#保存、打包" class="headerlink" title="保存、打包"></a>保存、打包</h3><p>保存，退出IDE，进入文件夹</p>
<p>将该文件夹下所有文件加入一个压缩包</p>
<h2 id="开通阿里云函数计算-Function-Compute"><a href="#开通阿里云函数计算-Function-Compute" class="headerlink" title="开通阿里云函数计算 Function Compute"></a>开通阿里云函数计算 Function Compute</h2><p>登录你的阿里云账号，进入<a target="_blank" rel="noopener" href="https://www.aliyun.com/product/fc">此网页</a>开通。</p>
<p>阿里云函数计算计费项及其免费额度如下表：</p>
<table>
<thead>
<tr>
<th align="left">计费项</th>
<th align="left">价格</th>
<th align="left">免费额度</th>
</tr>
</thead>
<tbody><tr>
<td align="left">调用次数（次）</td>
<td align="left">0.00000133（元）</td>
<td align="left">1,000,000（次）</td>
</tr>
<tr>
<td align="left">执行时间（CU-秒）</td>
<td align="left">0.000110592（元）</td>
<td align="left">400,000（CU-秒）</td>
</tr>
<tr>
<td align="left">函数内数据传输流量</td>
<td align="left">0.8（元）</td>
<td align="left">0</td>
</tr>
<tr>
<td align="left">函数请求响应流量</td>
<td align="left">0.5（元）</td>
<td align="left">0</td>
</tr>
<tr>
<td align="left">CDN回源流量</td>
<td align="left">0.5（元）</td>
<td align="left">0</td>
</tr>
</tbody></table>
<p>我们使用的流量小到几乎可以忽略不计。。。</p>
<hr>
<p>选择 <strong>服务及函数–&gt;新增服务</strong></p>
<img src="https://tc.skyone.host/blog/post/使用阿里云函数计算免费创建自己的音乐播放器API/新增服务.png" alt="新增服务" style="zoom: 50%;" />

<ul>
<li>服务名称：随意</li>
<li>描述：可填可不填</li>
<li>绑定日志：如果开启了日志服务可以勾选，同样有免费额度</li>
<li>链路追踪不开启<del>我穷，没钱</del></li>
</ul>
<p>如果开启了绑定日志，可能会让你授权，点击确认即可。</p>
<img src="https://tc.skyone.host/blog/post/使用阿里云函数计算免费创建自己的音乐播放器API/新增服务-表单.png" alt="新增服务-表单" style="zoom: 50%;" />

<p>然后<strong>新增函数–&gt;HTTP函数</strong></p>
<img src="https://tc.skyone.host/blog/post/使用阿里云函数计算免费创建自己的音乐播放器API/新增函数.png" alt="新增函数" style="zoom: 50%;" />

<ul>
<li>函数名：随意</li>
<li>环境：默认即可</li>
<li>上传代码，选择之前的压缩包</li>
<li>函数入口：格式<code>文件.函数名</code></li>
<li>打开高级设置</li>
<li>选弹性实例<del>我没钱</del></li>
<li>运行内存：最小的128MB都太多，10MB都不需要</li>
<li>超时时间：大于1秒即可</li>
<li>触发器名称：随意</li>
<li>认证方式：<code>anonymous</code></li>
<li>请求方式：改为<code>GET</code></li>
</ul>
<img src="https://tc.skyone.host/blog/post/使用阿里云函数计算免费创建自己的音乐播放器API/新增函数-表单.png" alt="新增函数-表单" style="zoom: 50%;" />

<p>进入该函数，选择触发器，复制链接</p>
<img src="https://tc.skyone.host/blog/post/使用阿里云函数计算免费创建自己的音乐播放器API/触发器链接.png" alt="触发器链接" style="zoom:50%;" />

<p>打开浏览器，粘贴链接，并在后面加上你的GET请求参数，我的例子可以用：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">?_=|https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/music/%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.mp3|https://ooo.0x0.ooo/2021/02/14/o08AS.png||https://cdn.jsdelivr.net/gh/skyone-res/static@master/res/lrc/%E5%A4%9C%E8%88%AA%E6%98%9F%20-%20%E6%B4%9B%E5%A4%A9%E4%BE%9D.lrc</span><br></pre></td></tr></table></figure>
<p>如果一切正常，会自动下载一个文件，用编辑器打开发现是对应的HTML代码，好的，进入下一步</p>
<h2 id="配置域名"><a href="#配置域名" class="headerlink" title="配置域名"></a>配置域名</h2><p>选择<strong>自定义域名–&gt;创建域名</strong></p>
<p>输入你的<strong>已经备案的</strong>域名，需要开启HTTPS的自行百度这么申请SSL证书</p>
<p>路径填 <code>/</code> ，当然你懂的话自行修改</p>
<p>选择函数为刚才创建的，LATEST版</p>
<p><img src="https://tc.skyone.host/blog/post/%E4%BD%BF%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91%E5%87%BD%E6%95%B0%E8%AE%A1%E7%AE%97%E5%85%8D%E8%B4%B9%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8API/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%9F%9F%E5%90%8D.png" alt="自定义域名"></p>
<p>点确定后，进入你的域名服务商的域名解析控制台，为你的主域名添加对应的子域名，选择CNAME模式，指向上图复制的链接。</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>打开如下网址</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http:&#x2F;&#x2F;你的域名&#x2F;你的get请求参数</span><br></pre></td></tr></table></figure>
<p>显示播放器即为成功。</p>
<p>嵌入网页：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">iframe</span> <span class="attr">style</span>=<span class="string">&quot;border:none:width:100%;height:110px;&quot;</span> <span class="attr">href</span>=<span class="string">&quot;http://你的域名/你的get请求参数&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">iframe</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="示例链接"><a href="#示例链接" class="headerlink" title="示例链接"></a>示例链接</h2><p>你可以用我建好的API试试</p>
<ul>
<li>URL：<code>https://music.luotianyi.space</code> </li>
<li>参数在<a href="#%E6%88%91%E7%9A%84API%E7%9A%84%E5%8F%82%E6%95%B0">这一段</a>有写</li>
</ul>
<p>例图：</p>
<p><a href="https://www.skyone.host/2021/02/24/win10-xia-pei-zhi-shu-mei-pai-pico-de-micropython-yu-c-huan-jing/"><img src="https://tc.skyone.host/blog/post/%E4%BD%BF%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91%E5%87%BD%E6%95%B0%E8%AE%A1%E7%AE%97%E5%85%8D%E8%B4%B9%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8API/%E4%BE%8B%E5%9B%BE.png" alt="例图"></a></p>
<p><strong>注意：</strong>不保证我提供的示例会稳定运行，如果我发现有很多来自非我网站的调用，我会停止此API，<strong>所以，想要就自己动手，不要什么事都让被人给你做好！</strong></p>
<p><span style="color:red;font-size:1.2em;">本文完全原创，禁止转载</span>，如果真的想转载，请<a href="mailto:wzw@skyone.host">邮箱联系我</a>。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E5%A4%9C%E8%88%AA%E6%98%9F.flac'></li>
                        
                    
                        
                            <li title='1' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E8%8C%89%E8%8E%89%E8%8A%B1.mp3'></li>
                        
                    
                        
                            <li title='2' data-url='https://tc.skyone.host/blog/music/%E8%B5%A4%E7%BE%BD%20-%20%E4%B8%87%E5%8F%A4%E7%94%9F%E9%A6%99.flac'></li>
                        
                    
                        
                            <li title='3' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E4%B8%BA%E8%B0%81%E8%80%8C%E4%B8%BA.mp3'></li>
                        
                    
                        
                            <li title='4' data-url='https://tc.skyone.host/blog/music/%E4%B9%90%E6%AD%A3%E7%BB%AB%20-%20Faded.mp3'></li>
                        
                    
                        
                            <li title='5' data-url='https://tc.skyone.host/blog/music/bilibili2018%20-%20%E8%87%B3%E5%B0%8A%E9%A9%AC%E7%94%B2.flac'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='d5e197ef955b7f3268e5'
        data-cs='7d4feed7a179ad28943b0865b7970814073145ad'
        data-r='blog-gitalk'
        data-o='skyone-wzw'
        data-a='skyone-wzw'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2"><span class="toc-number">1.</span> <span class="toc-text">写在前面</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%AE%E6%A0%87"><span class="toc-number">1.1.</span> <span class="toc-text">目标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">1.2.</span> <span class="toc-text">准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%8C%E6%88%90%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.3.</span> <span class="toc-text">完成示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%99%E4%BB%A3%E7%A0%81"><span class="toc-number">2.</span> <span class="toc-text">写代码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9C%8B%E6%96%87%E6%A1%A3"><span class="toc-number">2.1.</span> <span class="toc-text">看文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%99%E6%A8%A1%E6%9D%BF"><span class="toc-number">2.2.</span> <span class="toc-text">写模板</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85Node-js%E7%9A%84%E5%8C%85"><span class="toc-number">2.3.</span> <span class="toc-text">安装Node.js的包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E5%87%BD%E6%95%B0"><span class="toc-number">2.4.</span> <span class="toc-text">主函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%91%E7%9A%84API%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">2.5.</span> <span class="toc-text">我的API的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%9D%E5%AD%98%E3%80%81%E6%89%93%E5%8C%85"><span class="toc-number">2.6.</span> <span class="toc-text">保存、打包</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E9%80%9A%E9%98%BF%E9%87%8C%E4%BA%91%E5%87%BD%E6%95%B0%E8%AE%A1%E7%AE%97-Function-Compute"><span class="toc-number">3.</span> <span class="toc-text">开通阿里云函数计算 Function Compute</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%9F%9F%E5%90%8D"><span class="toc-number">4.</span> <span class="toc-text">配置域名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-number">5.</span> <span class="toc-text">测试</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B%E9%93%BE%E6%8E%A5"><span class="toc-number">6.</span> <span class="toc-text">示例链接</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/skyone.min.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>




</html>
